﻿import { Component, EventEmitter, Output, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormGroup, FormControl, Validators, FormBuilder }
    from '@angular/forms';
import { Http, Headers } from '@angular/http';
import { ApiService } from '../../services/app.service.api';
import { Movies } from '../../model/movies';
import { Genres } from '../../model/genres';
import { FileUploader } from 'ng2-file-upload';
@Component({
    selector: 'addmovie',
    templateUrl: './app/components/movies/addmovie.component.html'
})
export class AddMovieComponent implements OnInit {
    addMovieForm: FormGroup;
    @Output() showUserInfo = new EventEmitter();
    movie: Movies = new Movies();
    genres: Genres[];




    constructor(private apiService: ApiService, private router: Router) {
        this.movie.NumberOfStocks = 0;
    }

    AddMovie(): void {
        if (this.movie) {
            console.log(this.movie);
        }
        this.apiService.post('/api/movies/add', this.movie, res => {
            let movies: any = res.json();
            console.log(movies);
            //清空表单数据
            this.addMovieForm.reset();
        }, error => {

        });
    }

    loadGenres() {
        this.apiService.get('/api/genres/', {}, res => {
            this.genres = res.json() as Genres[];
            console.log(this.genres);
        }, error => {
            console.log(error);
        })
    }

    openDatePicker($event) {
        $event.preventDefault();
        $event.stopPropagation();
    };

    changeNumberOfStocks($event) {
        $event.preventDefault();
        $event.stopPropagation();
        var btn = $('#btnSetStocks'),
            oldValue = $('#inputStocks').val().trim(),
            newVal = 0;

        if (btn.attr('data-dir') == 'up') {
            newVal = parseInt(oldValue) + 1;
        } else {
            if (oldValue > 1) {
                newVal = parseInt(oldValue) - 1;
            } else {
                newVal = 1;
            }
        }
        $('#inputStocks').val(newVal);
        this.movie.NumberOfStocks = newVal;
        console.log(this.movie);
    }
    ngOnInit() {
        this.loadGenres();
    }
}




